﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;

namespace DC.DCHierarchy.DCResolvers.Client.DCResolverReference
{
    class CustomDataContractResolver : DataContractResolver
    {
        string NameSpace
        {
            get
            {
                return typeof(Customer).Namespace ?? "global";
            }
        }

        string Name
        {
            get
            {
                return typeof(Customer).Name;
            }
        }

        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            if (typeName == Name && typeNamespace == NameSpace)
            {
                return typeof(Customer);
            }
            else
            {
                return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
            }
        }


        public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out System.Xml.XmlDictionaryString typeName, out System.Xml.XmlDictionaryString typeNamespace)
        {
            //判断是否可以解析Customer类型
            if (type == typeof(Customer))
            {
                XmlDictionary dictionary = new XmlDictionary();

                //把类型名称和命名空间名称赋值给， typeName和typeNamespace输出参数
                typeName = dictionary.Add(Name);
                typeNamespace = dictionary.Add(NameSpace);

                return true;
            }
            else //如果不能解析该类型应该调用WCF默认的数据解析器
            {
                return knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace);
            }
        }
    }

}
